//
//  ViewController.m
//  movie
//
//  Created by MyMac on 15/4/5.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "MoviePlayer.h"
@interface ViewController ()
//@property(nonatomic,strong)AVPlayer*player;
//@property(nonatomic,strong)UIView *containerView;
//@property(nonatomic,strong)UIProgressView*progress;
@end

@implementation ViewController
- (IBAction)btn:(id)sender {



}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString*str=[NSHomeDirectory() stringByAppendingPathComponent:@"me"];
    NSLog(@"%@",str);
    MoviePlayer*moviePlayer=[[MoviePlayer alloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width, 400)];
    NSString*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"%@",path);
    NSLog(@"%@",moviePlayer);
    [self.view addSubview:moviePlayer];
    [moviePlayer.player play];
//    MoviePlayer*play=[[MoviePlayer alloc]createMoviePlayerFrame:CGRectMake(0, 0, self.view.bounds.size.width, 400)];
//    [self.view addSubview:play]
    
    
}
//-(void)setUI
//{
//    self.progress=[[UIProgressView alloc]initWithFrame:CGRectMake(0,350, self.view.bounds.size.width, 10)];
//    self.progress.progressTintColor=[UIColor redColor];
//    [self.view addSubview:self.progress];
//    AVPlayerLayer*player=[AVPlayerLayer playerLayerWithPlayer:self.player];
//    self.containerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
//    player.frame=self.containerView.bounds;
//    [self.view.layer addSublayer:self.containerView.layer];
//    player.backgroundColor=[[UIColor yellowColor] CGColor];
//    [self.containerView.layer addSublayer:player];
//    UIButton*btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [btn setTitle:@"开始" forState:UIControlStateNormal];
//    [btn setTitle:@"暂停" forState:UIControlStateSelected];
//    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//    btn.frame=CGRectMake(100, 300, 40, 20);
//    [self.view addSubview:btn];
//    
//    
//    
//}
//-(void)btnClick:(UIButton*)btn
//{
//    btn.selected=!btn.selected;
//    
//    
//}
//-(void)addProgressObserver{
//    AVPlayerItem *playerItem=self.player.currentItem;//当前的item，即当前的视频
//    UIProgressView *progress=self.progress;
//    //这里设置每秒执行一次
//    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
//        float current=CMTimeGetSeconds(time);
//        float total=CMTimeGetSeconds([playerItem duration]);
//        NSLog(@"当前已经播放%.2fs.",current);
//        if (current) {
//            [progress setProgress:(current/total) animated:YES];
//        }
//    }];
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//-(AVPlayer*)player
//{
//    if(!_player){
//        AVPlayerItem*playerItem=[AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"]];//AVPlayerItem一个媒体资源管理对象，管理着媒体视频的状态，信息，一个AVPlayerItem对应一个视频资源
//        _player=[AVPlayer playerWithPlayerItem:playerItem];
//        [self addPlayerFinishedNotifacation];
//        [self addProgressObserver];
//        [self addObserverToPlayerItem:playerItem];
//    
//    }
//    return _player;
//
//}
//-(void)addObserverToPlayerItem:(AVPlayerItem *)playerItem{
//    //监控状态属性，注意AVPlayer也有一个status属性，通过监控它的status也可以获得播放状态
//    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//    //监控网络加载情况属性
//    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
//}
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
//    AVPlayerItem *playerItem=object;
//    if ([keyPath isEqualToString:@"status"]) {
//        AVPlayerStatus status= [[change objectForKey:@"new"] intValue];
//        if(status==AVPlayerStatusReadyToPlay){
//            NSLog(@"正在播放...，视频总长度:%.2f",CMTimeGetSeconds(playerItem.duration));
//        }
//    }else if([keyPath isEqualToString:@"loadedTimeRanges"]){
//        NSArray *array=playerItem.loadedTimeRanges;
//        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间范围
//        float startSeconds = CMTimeGetSeconds(timeRange.start);
//        float durationSeconds = CMTimeGetSeconds(timeRange.duration);
//        NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
//        NSLog(@"共缓冲：%.2f",totalBuffer);
//        //
//    }
//}
//-(void)addPlayerFinishedNotifacation
//{
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playerFinished) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
//     //AVPlayerItem控制视频播放状态，所以通知是AVPlayerItem
//
//
//
//}
//-(void)playerFinished
//{
//    NSLog(@"视频播放完成");
//
//
//}
@end
